package metrics;
import java.awt.image.BufferedImage;

public class PSNR {

	public double compararImagens(BufferedImage img1, BufferedImage img2) {		
		//double sinal = 0;
		double ruido = 0;
		int rgb1 = 0;
		int rgb2 = 0;
		int r1 = 0;
		int g1 = 0;
		int b1 = 0;
		int r2 = 0;
		int g2 = 0;
		int b2 = 0;
		double luma1 = 0;
		double luma2 = 0;
		for (int i = 0; i < img1.getWidth(); i++) {
			for (int j = 0; j < img1.getHeight(); j++) {
				rgb1 = img1.getRGB(i, j);
				rgb2 = img2.getRGB(i, j);
				r1 = (rgb1 >> 16) & 0xff;
				g1 = (rgb1 >> 8) & 0xff;
				b1 = (rgb1) & 0xff;
				r2 = (rgb2 >> 16) & 0xff;
				g2 = (rgb2 >> 8) & 0xff;
				b2 = (rgb2) & 0xff;
				luma1 = 0.299 * r1 + 0.587 * g1 + 0.114 * b1;
				luma2 = 0.299 * r2 + 0.587 * g2 + 0.114 * b2;
				//sinal += luma1 * luma1;
				ruido += (luma1 - luma2) * (luma1 - luma2);
			}
		}

		double mse = ruido / (img1.getWidth() * img1.getHeight());

		double psnr = 10 * Math.log(255 * 255 / mse) / Math.log(10);

		return Math.ceil((psnr + 0.00001) * 100) / 100;

	}
}
